package com.android.miniskirt.ui;

import org.androidpn.client.ServiceManager;

import com.android.miniskirt.R;
import com.android.miniskirt.miniApplication;
import com.android.miniskirt.ui.miniskirt;
import com.android.miniskirt.service.getcontacts;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

@SuppressWarnings("unused")
public class PageLoad extends Activity {

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(receiver);
		mMiniApplication.doUnbindService();
	}

	private final contactsReceiver receiver = new contactsReceiver();
	private miniApplication mMiniApplication;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Application app = getApplication();
        if (app instanceof miniApplication)
        {
        	mMiniApplication = (miniApplication)app;
        	if(mMiniApplication.listItem != null) {
        		startActivity(new Intent(PageLoad.this, minitab.class));
    			PageLoad.this.finish();
        	}else {
        		setContentView(R.layout.pageload);
                IntentFilter filter = new IntentFilter(getcontacts.ACTION);
                registerReceiver(receiver, filter);
                LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
                AutoBackground(this, layout, R.drawable.bg_v, R.drawable.bg_h);
        	}	
        }
    }
    
    public static int ScreenOrient(Activity activity)
    {
        int orient = activity.getRequestedOrientation(); 
        if(orient != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && orient != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
             WindowManager windowManager = activity.getWindowManager();  
             Display display = windowManager.getDefaultDisplay();  
             int screenWidth  = display.getWidth();  
             int screenHeight = display.getHeight();  
             orient = screenWidth < screenHeight ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        }
        return orient;
    }
    
    public static void AutoBackground(Activity activity,View view,int Background_v, int Background_h)
    {
        int orient=ScreenOrient(activity);
        if (orient == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            view.setBackgroundResource(Background_v);
        }else{
            view.setBackgroundResource(Background_h);
        }  
    }
    
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		unregisterReceiver(receiver);
	} 
    
	public class contactsReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			startActivity(new Intent(PageLoad.this, minitab.class));
			Toast toast = Toast.makeText(getApplicationContext(), "receiver", Toast.LENGTH_LONG);
			toast.show();
			PageLoad.this.finish();
		}
		
	}
}